package sort

type Counter struct {
	BaseSort
}

// Sort 排序时放到临时数组里面了
func (self Counter) Sort() {
	if self.Array == nil || self.Len() < 2 {
		return
	}
	max := self.getMax()
	bucketLen := max + 1

	bucket := make([]int, bucketLen)

	for _, e := range self.Array {
		bucket[e]++
	}

	sortedIndex := 0
	for i := 0; i < bucketLen; i++ {
		for bucket[i] > 0 {
			self.Array[sortedIndex] = i
			bucket[i]--
			sortedIndex++
		}
	}
}

func (self Counter) getMax() int {
	var max int
	for _, e := range self.Array {
		if e > max {
			max = e
		}
	}
	return max
}
func (self Counter) Name() string {
	return "计数排序"
}
